package com.sz.framework.trace;

import com.sz.framework.threadlocal.SzThreadLocal;
import com.sz.framework.utils.CommonUtil;

import java.util.UUID;

/**
 * uuid生成traceId
 *
 * @author: yxl
 * 2025-07-24
 */
public class SzUUIDTraceIdDetector implements SzTraceIdDetector{

    private SzThreadLocal<String> threadLocal;

    public SzUUIDTraceIdDetector(SzThreadLocal<String> threadLocal) {
        this.threadLocal = threadLocal;
    }

    @Override
    public String get() {
        String traceId = threadLocal.get();
        if(CommonUtil.isEmpty(traceId)){
            threadLocal.set(UUID.randomUUID().toString().replaceAll("-", ""));
        }
        return threadLocal.get();
    }

    @Override
    public void clear() {
        threadLocal.clear();
    }
}
